<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/22
 * Time: 下午5:54
 */

namespace App\Http\Controllers;

use App\Http\Model\Hint;
use App\Http\Model\OutJson;

use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomScoreServices;
use App\Http\Services\RoomServicesInit;
use App\Http\Services\RoomVoteServices;
use App\Http\Services\RoomWaitbitServices;
use Illuminate\Http\Request;

/**
 * 房间投票
 */
class RoomVoteController extends Controller
{
    private $services;

    public function __construct()
    {
        $this->services = new RoomVoteServices();
    }

    /**
     * 读取投票信息
     * @param Request $request
     */
    public function getInit(Request $request)
    {
        $roomId = $request->get("room_id", ""); //房间ID
        if (empty($roomId)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $data = $this->services->getInit($roomId);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 设置房间信誉分
     * @param Request $request
     */
    public function start(Request $request)
    {
        $roomId = $request->get("room_id", 0); //房间ID
        $type = $request->get("type", 0); //操作类型：1按人气票;2按钻石礼物
        $topicId = $request->get("topic_id", 0); //话题id
        $voteNum = $request->get("vote_num", 0); //每人票数
        $voteTime = $request->get("vote_time", 0); //投票时长（分钟）
        $userIds = $request->get("user_ids", ''); //被投票的用户列表
        if (empty($roomId) || empty($type) || empty($voteTime) || empty($userIds)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $userIds = explode(',', $userIds);
        array_walk($userIds, 'intval');

        $voteTime *= 60; //分钟转秒
        $data = $this->services->startRoomVote($roomId, $type,$topicId, $voteNum, $voteTime, $userIds);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 取消投票
     * @param Request $request
     */
    public function cancel(Request $request)
    {
        $voteId = $request->get("vote_id", ""); //投票ID
        if (empty($voteId)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $data = $this->services->cancelRoomVote($voteId);
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 用户投票（人气票）
     * @param Request $request
     */
    public function poll(Request $request)
    {
        $voteId = $request->get("vote_id", ""); //房间ID
        $toUserId = $request->get("to_user_id", 0); //投票给用户ID

        if (empty($voteId) || empty($toUserId)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }

        $data = $this->services->pollRoomVote($voteId, intval($toUserId));
        OutJson::outJsonResult(Hint::SUCCESS, $data);
    }

    /**
     * 结束投票
     * @param Request $request
     */
    public function end(Request $request)
    {
        $voteId = $request->get("vote_id", ""); //投票ID
        if (empty($voteId)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
            return;
        }
        $result = $this->services->endRoomVote($voteId);
        if (!$result) {
            OutJson::outJsonNoData(Hint::ROOM_VOTE_NOT_EXISTS);
        }
        OutJson::outJsonResult(Hint::SUCCESS, []);
    }

    /**
     * 读取话题列表
     * @param Request $request
     */
    public function topics(Request $request)
    {
        OutJson::outJsonResult(Hint::SUCCESS, $this->services->topics());
    }

    /**
     * 添加/编辑 话题
     */
    public function editTopic(Request $request)
    {
        $id = $request->post("id", 0);
        $title = $request->post("title", "");

        if ($title == "") {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
        }

        $result = $this->services->editTopic($id, $title);
        OutJson::outJsonResult(Hint::SUCCESS, $result);
    }

    /**
     * 添加话题
     */
    public function deleteTopic(Request $request)
    {
        $id = $request->post("id", 0);
        if (empty($id)) {
            OutJson::outJsonNoData(Hint::PARAM_ERROR);
        }
        $result = $this->services->deleteTopic($id);
        OutJson::outJsonResult(Hint::SUCCESS, $result);
    }
}